const jwt = require('jsonwebtoken');

module.exports.authenticate = (req, res, next) => {
  const token = req.headers.authorization?.split(' ')[1];
  console.log(token);
  if (!token) {
    return res.status(401).json({ message: '未提供认证令牌' });
  }

  try {
    console.log(process.env.JWT_SECRET,"secret",token);
    
    const decoded = jwt.verify(token, process.env.JWT_SECRET);
    req.userId = decoded.id;
    next();
  } catch (err) {
    return res.status(401).json({ message: '无效令牌' });
  }
};